const pool = require('../utils/utils.js')
const jwt = require('jsonwebtoken')

module.exports = async function(ctx, next) {
    if(ctx.headers.token){
        let token = ctx.headers.token
        jwt.verify(token,"cczztt",(err,encoded)=>{
            console.log(err,encoded)
        })
    }
    const {pageNum=0,pageSize=10} = ctx.query
    let sqlStr = 'SELECT * FROM book ORDER BY rankNum DESC'
    if(ctx.query.bookName){
        sqlStr = 'SELECT * FROM book where bookName like '+JSON.stringify(`%${ctx.query.bookName}%`) + ' ORDER BY rankNum DESC AND isDelete=0'
    }
    console.log(sqlStr);
    var res = await new Promise((resolve, reject) => {
        pool.query(sqlStr, (err, result) => {
            if (err) {
                reject(err)
            } else {
                // console.log(result)
                resolve(result)
            }
        })
    })
    ctx.body = res
    await next()
}